<?php
/**
 * Created by PhpStorm.
 * User: ownfi
 * Date: 2017-03-13
 * Time: 19:45
 */

namespace App\Services\Impl;

use App\Common\Constant\CacheKey;
use App\Models\Province;
use App\Models\SmsLog;
use App\Services\IAreaService;
use App\Services\ISystemService;
use Cache;

class AreaServiceImpl implements IAreaService
{
    public function areaList(array $wheres = [])
    {
        $model = Province::alias('t1')->select('t1.*')->orderBy('t1.id','DESC')->with(['parent']);
        if ($wheres){
            foreach ($wheres as $where){
                if ($where[0] == 'parent'){
                    $model->join(Province::table().' as p', function ($join) use ($where){
                        $join->on('t1.parentid', '=', 'p.id')
                            ->where('p.name',$where[1],$where[2]);
                    });
                }else if ($where[0] == 'name'){
                    $model->where('t1.name',$where[1],$where[2]);
                }else{
                    $model->where([$where]);
                }
            }
        }
        return $model->paginate(config('common.perpage'));
    }

    public function areaAdd($parent_id, $name)
    {
        return Province::create([
            'parentid'=>$parent_id,
            'name'=>$name,
        ]);
    }

    public function areaEdit($area_id, $parent_id, $name)
    {
        return Province::find($area_id)->update([
            'parentid'=>$parent_id,
            'name'=>$name,
        ]);
    }

    public function areaDel($area_id)
    {
        return Province::find($area_id)->delete();
    }

    public function getAreaCrumbById($id, $glue = '')
    {
        $areas = getAreas();
        $crumbs = [];
        while(isset($areas[$id])){
            array_unshift($crumbs,$areas[$id]['n']);
            $id = @$areas[$id]['p'];
        }
        return implode($glue,$crumbs);
    }

    public function getProvinceCouple()
    {
        $cacheKey = CacheKey::provinceListKey();
        $timeoutMinutes = 1440;
        return Cache::remember($cacheKey, $timeoutMinutes, function () {
            return Province::where('parentid',0)->get()->mapWithKeys(function ($item, $key) {
                return [$item->id=>$item->name];
            });
        });
    }

    public function getCityCouple($provinceId)
    {
        $cacheKey = CacheKey::provinceCityListKey($provinceId);
        $timeoutMinutes = 1440;
        return Cache::remember($cacheKey, $timeoutMinutes, function () use ($provinceId){
            return Province::where('parentid',$provinceId)->get()->mapWithKeys(function ($item, $key) {
                return [$item->id=>$item->name];
            });
        });
    }

    public function getDistrictCouple($cityId)
    {
        $cacheKey = CacheKey::provinceCityListKey($cityId);
        $timeoutMinutes = 1440;
        return Cache::remember($cacheKey, $timeoutMinutes, function () use ($cityId){
            return Province::where('parentid',$cityId)->get()->mapWithKeys(function ($item, $key) {
                return [$item->id=>$item->name];
            });
        });
    }

    public function getAreaById($id)
    {
        return Province::find($id);
    }

    public function getAreasByParentId($id)
    {
        return Province::find($id)->children;
    }

    public function getParentArea($id)
    {
        return Province::find($id)->parent;
    }

    public function getProvinces()
    {
        return Province::where('parentid',0)->get();
    }

    public function isProvince($id)
    {
        return Province::find($id)->parentid == 0;
    }

    public function isCity($id)
    {
        return Province::find($id)->parent->parentid == 0;
    }

    public function isDistrict($id)
    {
        return Province::where('parentid',$id)->count() == 0;
    }

    public function getParentsCouple($id)
    {
        $crumbs = [];
        do{
            $model = Province::find($id);
            $crumbs[$id] = $model->name;
            $id = $model->parentid;
        }while($model->parentid != 0);
        return array_reverse($crumbs,true);
    }

}